package com.ruoyi.washer.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.washer.domain.XwDevice;
import com.ruoyi.washer.mapper.XwDeviceMapper;
import com.ruoyi.washer.mapper.XwDivideAccountsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.washer.mapper.XwEnduserMapper;
import com.ruoyi.washer.domain.XwEnduser;
import com.ruoyi.washer.service.IXwEnduserService;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 终端经营用户Service业务层处理
 *
 * @author hawking
 * @date 2023-12-15
 */
@Service
public class XwEnduserServiceImpl implements IXwEnduserService
{
    @Autowired
    private XwEnduserMapper xwEnduserMapper;

    @Autowired
    private XwDeviceMapper xwDeviceMapper;

    @Autowired
    private XwDivideAccountsMapper xwDivideAccountsMapper;

    /**
     * 查询终端经营用户
     *
     * @param enduserId 终端经营用户主键
     * @return 终端经营用户
     */
    @Override
    public XwEnduser selectXwEnduserByEnduserId(Long enduserId)
    {
        return xwEnduserMapper.selectXwEnduserByEnduserId(enduserId);
    }

    /**
     * 查询终端经营用户列表
     *
     * @param xwEnduser 终端经营用户
     * @return 终端经营用户
     */
    @Override
    public List<XwEnduser> selectXwEnduserList(XwEnduser xwEnduser)
    {
        return xwEnduserMapper.selectXwEnduserList(xwEnduser);
    }

    /**
     * 新增终端经营用户
     *
     * @param xwEnduser 终端经营用户
     * @return 结果
     */
    @Override
    public int insertXwEnduser(XwEnduser xwEnduser)
    {
        return xwEnduserMapper.insertXwEnduser(xwEnduser);
    }

    /**
     * 修改终端经营用户
     *
     * @param xwEnduser 终端经营用户
     * @return 结果
     */
    @Override
    public int updateXwEnduser(XwEnduser xwEnduser)
    {
        return xwEnduserMapper.updateXwEnduser(xwEnduser);
    }

    /**
     * 批量删除终端经营用户
     *
     * @param enduserIds 需要删除的终端经营用户主键
     * @return 结果
     */
    @Override
    public int deleteXwEnduserByEnduserIds(Long[] enduserIds)
    {
        return xwEnduserMapper.deleteXwEnduserByEnduserIds(enduserIds);
    }

    /**
     * 删除终端经营用户信息
     *
     * @param enduserId 终端经营用户主键
     * @return 结果
     */
    @Override
    public int deleteXwEnduserByEnduserId(Long enduserId)
    {
        return xwEnduserMapper.deleteXwEnduserByEnduserId(enduserId);
    }
    public XwEnduser getEnduserMessage(String openId){
        return xwEnduserMapper.selectXwEnduserByOpenId(openId);
    }

    public JSONObject getEnduserDeviceList(String openId){
        JSONObject jsonObject=new JSONObject();
        XwDevice xwDevice=new XwDevice();
        xwDevice.setEnduserOpenId(openId);
        List<XwDevice> xwDeviceList= xwDeviceMapper.selectXwDeviceListEnduer(xwDevice);
        jsonObject.put("xwDeviceList",xwDeviceList);
        if(xwDeviceList!=null&&xwDeviceList.size()>0){
            int b4G=0;
            int bluetooth=0;
            int allCount=xwDeviceList.size();
            for(int i=0;i<allCount;i++){
                XwDevice xwDevice1=xwDeviceList.get(i);
                if(xwDevice1.getDeviceType().equals("1")){
                    b4G++;
                }else{
                    bluetooth++;
                }
            }
            jsonObject.put("b4G",b4G);
            jsonObject.put("bluetooth",bluetooth);
            jsonObject.put("allCount",allCount);
            String currentMonth = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM"));
            jsonObject.put("currentMonthDivideAmount", xwDivideAccountsMapper.getMonthDivideAmountByOpenIdAndDivideState(currentMonth, openId, "Y"));
            String previousMonth = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM"));
            jsonObject.put("previousMonthDivideAmount", xwDivideAccountsMapper.getMonthDivideAmountByOpenIdAndDivideState(previousMonth, openId, "Y"));
        } else {
            jsonObject.put("b4G", 0);
            jsonObject.put("bluetooth", 0);
            jsonObject.put("allCount", 0);
            jsonObject.put("currentMonthDivideAmount", 0);
            jsonObject.put("previousMonthDivideAmount", 0);
        }
        return jsonObject;
    }

    public int saveEnduserMessage(XwEnduser xwEnduser){
        XwEnduser xwEnduser1=new XwEnduser();
        xwEnduser1.setEnduserOpenId(xwEnduser.getEnduserOpenId());
        List<XwEnduser> xwEnduserList=xwEnduserMapper.selectXwEnduserList(xwEnduser1);
        if(xwEnduserList!=null&&xwEnduserList.size()>0){
            return 1;
        }else{
            xwEnduser.setCreatedTime(new Date());
            xwEnduser.setFreezeState("N");
            xwEnduser.setCashoutType("1");
            xwEnduserMapper.insertXwEnduser(xwEnduser);
        }
        return 2;
    }
    public int saveEnduserCashoutSettings(String cashoutType,
                                   String enduserMerchantCode,
                                   String enduserMerchantName,
                                   String openId){
        XwEnduser xwEnduser=xwEnduserMapper.selectXwEnduserByOpenId(openId);
        xwEnduser.setCashoutType(cashoutType);
        xwEnduser.setEnduserMerchantCode(enduserMerchantCode);
        xwEnduser.setEnduserMerchantName(enduserMerchantName);
        return xwEnduserMapper.updateXwEnduser(xwEnduser);
    }
}
